package co.gov.medellin.ssti.sub.model.dao.impl;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;

public final class DAOUtil {
	// static final private Log log = LogFactory.getLog(DAOUtil.class);

	private DAOUtil() {
	}

	public static UserTransaction getUserTransaction() {
		Object obj = lookup("java:comp/UserTransaction");
		UserTransaction tx = (UserTransaction) narrow(obj,
				UserTransaction.class);
		return tx;
	}

	public static Object lookup(final String name) {
		Context ctx = getInitialContext();

		Object result = null;

		try {
			result = ctx.lookup(name);
		} catch (NamingException ex) {
			throw new DAOException(ex);
		}

		return result;
	}

	public static Context getInitialContext() {
		Context ctx = null;

		try {
			ctx = new InitialContext();
		} catch (NamingException ex) {
			throw new DAOException(ex);
		}
		return ctx;
	}

	public static Object narrow(final Object obj, final Class clazz) {
		return javax.rmi.PortableRemoteObject.narrow(obj, clazz);
	}

}
